// challeng/pages/qplay/qplay.js
var app = getApp();
Page({

  data: {
	  game_bg:'',
		gametoken:'',
		openid:'',
		mem:{},
		total:0,
		cur_num:0,
		times:[],
		g_nums:[],
		resettime:0,
		answerIng:false,
		beginCountDown: 3,//游戏开始--倒计时
		gamingStatus: 'notBegin',//游戏状态4个。notBegin未开始，running正在运行，fail挑战失败,success挑战成功
		
		bgActiveTime: 120, // 背景转动时所用总时长，单位毫秒
	    bgActiveAngle: 0, // 背景转动的角度，要是45的倍数
	    qwActiveTime: 1000, // 青蛙移动时长，单位毫秒
	    qwActiveSite: 0, // 青蛙便宜位置，只能取值 0 或 370
	    isactive:0,
	    selactive:0,
	    isfirst:0,
  },
 
  onLoad: function (options) {
	  var that = this;
	  wx.showLoading({title:'数据加载中...'});
	  app.sysInit(this,function(res){
		  app.getUserInfo(function (res) {
			  var openid = res['openid'];
			  //获取分享内容
			  app.getShareData(function(res){
				  that.setData({shareData:res.data});
			  },openid,8);
			  
			  that.setData({openid:openid});
			  app.util.request({
				  url: 'entry/wxapp/qplay',
				  data: {
					  m: 'junsion_challeng',
					  t: 1,
					  openid:openid,
					  game:8,
				  },
				  success: function (res) {
					  wx.hideLoading();
					  if(res.data.code==0 || res.data.code==2){
						  var code = res.data.code;
						  wx.showModal({
							  title:res.data.msg,
							  showCancel:false,
							  success: function(res) {
								  wx.showLoading({title:'数据加载中...'});
								  if(code==2)
									  var url = '../index/index?gobuy=1';
								  else var url = '../index/index';
								  wx.redirectTo({
									  url: url
								  })
							  }
						  });
					  }
					  else{
						  that.setData({
							  game_bg:res.data.game_bg,
							  gametoken:res.data.gametoken,
							  mem:res.data.mem,
							  beginCountDown:res.data.down,
							  total:res.data.total,
							  g_nums:res.data.g_nums,
							  times:res.data.times,
							  answerIng:false,
						  });
						  if(parseInt(res.data.times[0])>0){
							  that.setData({
								  resettime:res.data.times[0],
							  });
						  }
						  else if(parseInt(res.data.times[1])>0){
							  that.setData({
								  resettime:res.data.times[1],
							  });
						  }
						  else if(parseInt(res.data.times[2])>0){
							  that.setData({
								  resettime:res.data.times[2],
							  });
						  }
						  else{
							  that.setData({
								  resettime:300,
							  });
						  }
						  that.beginCountDown(res.data.down);//游戏主入口
					  }
				  },
			  });
		  });
	  });
	  var music = wx.getStorageSync('music');
      that.setData({
		  music:music.music,
		  err_music:music.err_music,
	  });
  },
  
  onReady: function () {
	  var that=this;
  	  this.audioCtx = wx.createAudioContext('myAudio');
      this.errAudio = wx.createAudioContext('errAudio');
      this.audioCtx.play();
  },
  
  selColor:function(){
	  var that=this;
	  var activeAngle = parseInt(that.data.bgActiveAngle) + 45;
	  var selactive = parseInt(that.data.selactive) + 1;
	  selactive = selactive % 8;
	  that.setData({ bgActiveAngle: activeAngle,selactive:selactive });
  },
  
  //青蛙掉落动画
  qingwWa:function(){
	  var that=this;
	  var qwActiveSite = that.data.qwActiveSite;
	  if(qwActiveSite==0) qwActiveSite = 370;
	  else qwActiveSite = 0;
	  var isfirst = that.data.isfirst;
	  var answerIng = that.data.answerIng;
	  var resettime = parseInt(that.data.resettime);
	  if(isfirst==0){
		  var space_time = resettime / 2;
	  }
	  else{
		  var space_time = resettime;
	  }
	  if(qwActiveSite==370){
		  if(!answerIng)that.checkVal();
	  }
	  setTimeout(() => {
		  that.setData({
			  qwActiveSite: qwActiveSite
		  });
		  that.qingwWa();
	  }, parseInt(space_time))
  },

  checkVal:function(){
	  var that=this;
	  var isfirst = that.data.isfirst;
	  var selactive = parseInt(that.data.selactive);
	  var isactive = parseInt(that.data.isactive);
	  var cur_num = parseInt(that.data.cur_num);
	  if(selactive==isactive){
		  cur_num++;
		  if (cur_num == parseInt(that.data.total)) {
			  that.setData({ answerIng:true });
			  app.checkReward(that,8,cur_num);//中奖了
				return false;
		  }
		  //重新获取随机颜色
		  if(isfirst==0){
			  var now_isactive = 0;
			  that.setData({ isfirst:1 });
		  }
		  else{
			  var now_isactive = app.fRandomBy(0,7);
		  }
			if(parseInt(that.data.g_nums[0])>cur_num){
				var resettime=that.data.times[0];
			}
			else if(parseInt(that.data.g_nums[1])>cur_num){
				var resettime=that.data.times[1];
			}
			else if(parseInt(that.data.g_nums[2])>cur_num){
				var resettime=that.data.times[2];
			}
			else{
				var resettime=300;
			}
		  that.setData({ isactive:now_isactive,resettime:resettime,qwActiveTime:resettime,cur_num:cur_num });
	  }
	  else{
		  that.setData({ answerIng:true });
		  that.audioCtx.pause()
		  app.tzfail(that,8);//挑战失败
	  }
  },
  
//游戏开始倒计时
  beginCountDown:function(countTime){
      var that=this;
      var timer = setInterval(function () {
          that.setData({ beginCountDown: --countTime });
          if (countTime <= 0) {
              clearInterval(timer);
              that.setData({
                  gamingStatus:'running'
              });
              that.qingwWa();
          }
      }, 1000)
  },
  onShareAppMessage:function(){
	  var that = this;
	  var mid = that.data.mem.id;
	  var sharedata = that.data.shareData;
	  sharedata.path = 'challeng/pages/index/index?mid='+mid+'&gType=8';
	  return {
		  title: sharedata.title,
	     // desc: sharedata.desc,
	      path: sharedata.path,
	      imageUrl:sharedata.pic,
	      success(res){
	    	  var shareTicket = res.shareTickets[0] // 获取 shareTicket
	    	  wx.getShareInfo({
	              shareTicket: shareTicket,
	              complete(res) {
	            	  app.util.request({
		    	        url: 'entry/wxapp/addnums',
		    	        data: {
		    	            m : 'junsion_challeng',
		    	            t : 1,
		    	            mid : that.data.openid,
		    	            iv : res.iv,
		    	            encryptedData : res.encryptedData,
		    	        },
		    	        success: function (res) {
		    	        	if (res.data.code == 1) {
		                        that.setData({ challeng_num: parseInt(res.data.num)});
	                        	wx.showToast({
	                        		title:'次数\t+'+res.data.num,
	                        		icon:'none',
	                				mask:true,
	                				duration:1000
	                			});
		    	    	    } 
		    	        	else{
		    	        		 wx.showModal({title:res.data.msg,showCancel:false});
		    	        	}
		    	        }
		    	    }); 
	                
	              }
	          });
	      },
	      fail: function(res) {
	        // 转发失败
	    	  console.log(res);
	      }
	  }
  },
})